package cn.luyou.admin.controller;

import cn.luyou.admin.common.Result;
import cn.luyou.admin.dto.*;
import cn.luyou.admin.entity.ExamRecord;
import cn.luyou.admin.entity.ExamRecordDetail;
import cn.luyou.admin.service.ExamRecordService;
import cn.luyou.admin.utils.BaseContext;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam-records")
public class ExamRecordController {

    @Autowired
    private ExamRecordService examRecordService;

    @GetMapping
    public Result<IPage<ExamRecordDto>> getRecords(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String keyword) {
        return Result.success(examRecordService.getRecords(pageNum, pageSize, userId, keyword));
    }

    @PostMapping("/submit")
    public Result<ExamRecord> submitAnswers(@RequestBody @Valid SubmitAnswerDTO submitAnswerDTO, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(examRecordService.submitAnswers(submitAnswerDTO, BaseContext.getCurrentId()));
    }

    @GetMapping("/{id}/details")
    public Result<List<ExamRecordDetail>> getRecordDetails(@PathVariable Long id) {
        return Result.success(examRecordService.getRecordDetails(id));
    }

    @GetMapping("/wrong-questions")
    public Result<List<ExamRecordDetailDto>> getWrongQuestions(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(examRecordService.getWrongQuestions(BaseContext.getCurrentId()));
    }

    @GetMapping("/class-scores")
    public Result<List<ExamRecord>> getClassScores(
            @RequestParam String className,
            @RequestParam(required = false) Long paperId) {
        return Result.success(examRecordService.getClassScores(className, paperId));
    }

    @GetMapping("/user-scores")
    public Result<List<ExamRecord>> getUserScores(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(examRecordService.getUserScores(BaseContext.getCurrentId()));
    }

    @GetMapping("/class-statistics")
    public Result<ScoreStatisticsDTO> getClassScoreStatistics(
            @RequestParam String className,
            @RequestParam(required = false) Long paperId) {
        return Result.success(examRecordService.getClassScoreStatistics(className, paperId));
    }

    @GetMapping("/score-trend")
    public Result<List<ExamRecord>> getUserScoreTrend(
            HttpServletRequest request,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
//        Long userId = (Long) request.getAttribute();
        return Result.success(examRecordService.getUserScoreTrend(BaseContext.getCurrentId(), limit));
    }

    @GetMapping("/statistics")
    public Result<StatisticsDTO> getStatistics(HttpServletRequest request) {
        return Result.success(examRecordService.getStatistics(BaseContext.getCurrentId()));
    }
} 